#pragma once

#include <iostream>
#include <string>
#include <sys/types.h>  //socket() recvfrom()
#include <sys/socket.h> //socket() bind() recvfrom() inet_ntoa()
#include <cerrno>
#include <cstring>
#include <netinet/in.h> //inet_ntoa()
#include <arpa/inet.h>  // struct sockaddr_in inet_ntoa() ntohs() htonl()
#include <functional>

namespace Server
{
    using namespace std;
    static const string defaultIp = "0,0,0,0";
    static const int gnum = 1024;
    enum
    {
        USAGE_ERR = 1,
        SOCKET_ERR,
        BIND_ERR,
        OPEN_ERR
    };

    typedef function<void(int, string, uint16_t, string)> func_t;

    class udpServer
    {
    public:
        udpServer(const func_t &cb, const uint16_t &port, const string &ip = defaultIp)
            : _callback(cb), _port(port), _ip(ip), _sockfd(-1)
        {
        }

        void initServer()
        {
            // 1.创建socke
            _sockfd = socket(AF_INET, SOCK_DGRAM, 0);
            if (_sockfd == -1)
            {
                cerr << "socket error:" << errno << ":" << strerror(errno) << endl;
            }
            cout << "socket success: "
                 << " ；" << _sockfd << endl;
            // 2.绑定port、ip
            // 未来服务器要明确的port，不能随意改变
            struct sockaddr_in local;
            bzero(&local, sizeof(local));
            local.sin_family = AF_INET;
            local.sin_port = htons(_port);
            // local.sin_addr.s_addr = htonl(INADDR_ANY);  服务器不要绑定单独的ip
            //  任意地址bind，服务器的真实写法
            int n = bind(_sockfd, (struct sockaddr *)&local, sizeof(local));
            if (n == -1)
            {
                cerr << "bind error: " << errno << ":" << strerror(errno) << endl;
                exit(BIND_ERR);
            }
            // UDP Server 的预备工作完成
        }

        void start()
        {
            // 服务器本质就是一个死循环
            char buffer[gnum];
            for (;;)
            {
                // 读取数据
                struct sockaddr_in peer;
                socklen_t len = sizeof(peer);
                // 读取方式默认为0,阻塞式读取
                // 返回值：These calls return the number of bytes received, or -1  if  an  erroroccurred.
                ssize_t s = recvfrom(_sockfd, buffer, sizeof(buffer) - 1, 0, (struct sockaddr *)&peer, &len);

                // 1.数据是什么 2.是谁发的
                if (s > 0)
                {
                    buffer[s] = 0;
                    string clientip = inet_ntoa(peer.sin_addr); // inet_ntoa的作用，1.把网络序列转化为主机序列 2.把int型ip 转化为 点分十进制ip
                    uint16_t clientport = ntohs(peer.sin_port);
                    string message = buffer;

                    cout << clientip << "[" << clientport << "]#" << message << endl;

                    // 使用回调函数对读取到的数据做处理
                    _callback(_sockfd, clientip, clientport, message);
                }
            }
        }

        ~udpServer()
        {
        }

    private:
        uint16_t _port;
        string _ip; // 一个网络服务器在实际上，不建议指明一个IP
        int _sockfd;
        func_t _callback;
    };

}